package com.warmguard.core.controlller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.warmguard.core.core.vo.Result;
import com.warmguard.core.core.vo.ResultQueryPage;
import com.warmguard.core.domain.entity.Banners;
import com.warmguard.core.domain.entity.Category;
import com.warmguard.core.domain.entity.Product;
import com.warmguard.core.domain.vo.ProductDetailsVO;
import com.warmguard.core.service.BannersService;
import com.warmguard.core.service.CategoryService;
import com.warmguard.core.service.ProductService;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * ClassName: HomeController
 * PackageName: com.warmguard.core.controlller
 * Description:
 *
 * @Author: 32782
 * @Date: 2025/1/31 下午12:29
 * @Version: 1.0
 */
@RequestMapping("/member/home")
@RestController
public class HomeController {
    @Autowired
    private BannersService bannersService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private ProductService productService;

    @GetMapping("/banner")
    public Result getBanner() {
        LambdaQueryWrapper<Banners> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Banners::getType, 1);
        List<Banners> bannersList = bannersService.list();
        return Result.ok(bannersList);
    }

    @GetMapping("/category")
    public Result getCategory() {
        List<Category> categoryList = categoryService.list();
        return Result.ok(categoryList);
    }

    @GetMapping("/goods/guessLike")
    public Result getProduct(@RequestParam(value = "page", defaultValue = "1") int page,
                             @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {
        Page<Product> productPage = new Page<>(page, pageSize);
        Page<Product> resultPage = productService.page(productPage);
        ResultQueryPage<Product> queryPage = new ResultQueryPage<>();

        queryPage.setPage(resultPage.getCurrent());
        queryPage.setPageSize(resultPage.getSize());
        queryPage.setTotal(resultPage.getTotal());
        queryPage.setData(resultPage.getRecords());

        return Result.ok(queryPage);
    }

    @GetMapping("/search")
    public Result search(@RequestParam(value = "page", defaultValue = "1") int page,
                         @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                         @RequestParam(value = "keyWord", defaultValue = "") String keyWord) {
        Page<Product> productPage = new Page<>(page, pageSize);
        Page<Product> resultPage = productService.search(productPage, keyWord);
        ResultQueryPage<Product> queryPage = new ResultQueryPage<>();
        queryPage.setPage(resultPage.getCurrent());
        queryPage.setPageSize(resultPage.getSize());
        queryPage.setTotal(resultPage.getTotal());
        queryPage.setData(resultPage.getRecords());
        return Result.ok(queryPage);
    }

    @GetMapping("/goods/detail")
    public Result getProductDetail(@RequestParam(value = "id") Long id) {
        ProductDetailsVO productDetailsVO = productService.getProductWithDetailsById(id);
        return Result.ok(productDetailsVO);
    }


}
